<?php
class RM_Groups_Unit_Row extends RM_Unit_Row
{
    protected $_mainGroupUnit = null;

    public function isGroup()
    {
        if (isset($this->_data['group_id']) || $this->_data['group_id'] !== null){
            return $this->_data['group_id'];
        }
        return false;
    }

    public function isTemplateUnit(){
        if (isset($this->_data['group_id']) == false || $this->_data['group_id'] == null) {
            return;
        }
        $groupModel = new RM_Groups();
        $group = $groupModel->find($this->group_id)->current();
        $mainUnitID = $group->main_unit_id;
        if ($mainUnitID == null) {
            return;
        }
        if ($mainUnitID == $this->_data['id']) {
            return (int)$mainUnitID;
        }
        $unitModel = new RM_Units();
        $unit = $unitModel->get($mainUnitID);
        if ($unit == null) {
            return;
        }
        return (int)$unit->id;
    }

    public function getAllSubUnits(RM_Unit_Search_Criteria $criteria, RM_Unit_Row $masterUnit){
        $unitModel = new RM_Units();
        $criteria->availabilityCheckOnly = true;
        $criteria->groupsOnly = true;
        $criteria->mainUnit = $masterUnit;
        $units = $unitModel->getAll($criteria);
        return $units;
    }

    public function __sleep()
    {
        $parentFields = parent::__sleep();
        $parentFields[] = '_mainGroupUnit';
        return $parentFields;
    }

    public function init()
    {        
        if (RM_Environment::getInstance()->getConnector()->getModule() == 'admin') {
            return;
        }

        // if we have a session value of returnAllUnits abandon this method so
        // that all units are shown.
        if (isset($_SESSION["returnAllUnits"]) && $_SESSION["returnAllUnits"] == true){
            return;
        }

        if (isset($this->_data['group_id']) == false || $this->_data['group_id'] == null) {
            return;
        }
        $groupModel = new RM_Groups();
        $group = $groupModel->find($this->group_id)->current();
        $mainUnitID = $group->main_unit_id;
        if ($mainUnitID == null) {
            return;
        }
        if ($mainUnitID == $this->_data['id']) {
            return;
        }
        $unitModel = new RM_Units();
        $unit = $unitModel->get($mainUnitID);
        if ($unit == null) {
            return;
        }
        $this->_mainGroupUnit = $unit;
    }

    /**
     * Retrieve row field value
     *
     * @param  string $columnName The user-specified column name.
     * @return string             The corresponding column value.
     * @throws Zend_Db_Table_Row_Exception if the $columnName is not a column in the row.
     */
    public function __get($columnName)
    {
        if ($this->_mainGroupUnit == null) {
            return parent::__get($columnName);
        }
        return $this->_mainGroupUnit->$columnName;
    }

    public function __isset($columnName)
    {
        if ($this->_mainGroupUnit == null) {
            return parent::__isset($columnName);
        }
        return isset($this->_mainGroupUnit->$columnName);
    }

    /**
     * Returns the column/value data as an array.
     *
     * @return array
     */
    public function toArray()
    {
        if ($this->_mainGroupUnit == null) {
            return parent::toArray();
        }
        return $this->_mainGroupUnit->toArray();
    }
}